package edu.sjsu.cs.security;

public class SubstitutionCipher implements MessageEncoder, MessageDecoder
{
   public SubstitutionCipher(int shift)
   {
      ShiftKey = shift;
   }

   public String encode(String plaintext)
   {
      char myCharArray[] = plaintext.toCharArray();
      for(int i=0;i<plaintext.length();i++)
      {
         myCharArray[i] = shiftCharForward(myCharArray[i]);
        
      }
    
      return new String(myCharArray);
      
   }

   public String decode(String cipherText)
   {
      char myCharArray[] = cipherText.toCharArray();
      for(int i=0;i<cipherText.length();i++)
      {
         myCharArray[i] = shiftCharBackward(myCharArray[i]);
      }
    
      return new String(myCharArray);
   }
   private char shiftCharForward(char aChar)
   {
     int result = (int) aChar;
     result+=ShiftKey;
     return (char)result;
   }
   private char shiftCharBackward(char aChar)
   {
      int result = (int) aChar;
      result-=ShiftKey;
      return (char)result;
   }
   private int ShiftKey;
}
